<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Request;
use think\helper\Time;
use think\Loader;
use think\Image;

use app\index\model\ComValidate;
use app\index\model\ComModel;

class Common extends Controller{

	public $dir="./app/index/view/index/";

	public function __construct(){

		parent::__construct(); 
		
		/*return true;
*/
		//return true;
		if(!session('admin')){
			$this->redirect('login/index');
			//alert("请先登录！",Url('Admin/login'));
		}
	}
	
	#控制器初始化#
	/*public function _initialize(){
		
		//return true;
		if(!session('admin')){
			$this->redirect('login/index');
			//alert("请先登录！",Url('Admin/login'));
		}
		$sid=session('sid');

		$rule_mode=config('rule_mode');

		if($rule_mode=='2'){ //严格模式，每一步都要判断
			$groupid=Db::name('admin_list')->where('id',$sid)->value('groupid');
			$role=Db::name('admin_role')->where('id',$groupid)->find();
			if($role['status']!='1'){
				session('sid',null);
				alert("当前状态已被禁用，请重试！",Url('Admin/login'));
			}else{
				if($role['rule']!='alladmin'){ //非超级管理员
					$rulegroup=explode(",",$role['rule']);

					$request = Request::instance();

					$controller=$request->controller();
					$action=$request->action();
					$action=array(
						'controller'=>strtolower($controller),
						'action'=>strtolower($action),
						'status'=>"1"
					);

					$ruleid=Db::name('admin_rule_list')->where($map)->value('id');
					if(!$ruleid){ //不在权限里面，404
						//404
						exit;
					}
					if(!in_array($ruleid,$rulegroup)){
						alert("暂无权限！");
					}
				}
				
			}

		}else if($rule_mode=='1'){ //非严格模式,判断是否在session里面
			if(!session('rule')){
				$groupid=Db::name('admin_list')->where('id',$sid)->value('groupid');
				$role=Db::name('admin_role')->where('id',$groupid)->find();
				$rule=explode(",",$role['rule']);
				session('rule',$rule);
			}

			$request = Request::instance();

			$controller=$request->controller();
			$action=$request->action();

			$action=array(
				'controller'=>strtolower($controller),
				'action'=>strtolower($action),
				'status'=>"1"
			);

			$ruleid=Db::name('admin_rule_list')->where($map)->value('id');
			if(!$ruleid){ //不在权限里面，404
				//404
				exit;
			}
			if(!in_array($ruleid,session('rule'))){
				alert("暂无权限！");
			}

		}
		
    }*/

	#上传#
	public function fileupload(){
		if(request()->ispost()){
		
			$file = request()->file('file');


			$type=input('type');

			$catalog=input("catalog","file");

			//文件上传路径
			$file_up_path=ROOT_PATH . 'public' . DS . 'uploads'. DS .$catalog;

			//创建目录
			if(!is_dir($file_up_path)){
				mkdir ($file_up_path,0777,true);	
			}

			//图片上传
			if($type=='image'){

				$width=input('width');
				$height=input('height');

				$info = $file->move($file_up_path);
			    if($info){
			        // 成功上传后 获取上传信息
			       $path=$file_up_path. DS .$info->getSaveName();
			     
			       //缩略图
			       if($width || $height){
			       		$image = \think\Image::open($path);
			       		$image->thumb($width, $height,\think\Image::THUMB_CENTER)->save($path);
			       }
				  	
				   $arr=array(
				   		'path'=>DS .'public' . DS . 'uploads'. DS .$catalog. DS .$info->getSaveName(),
				   		'status'=>'1',
				   		'filename'=>$info->getInfo()['name'] //文件原来的名称
				   	);
			       return json($arr);
			    }else{
			    	return json($file->getError());
			    }

			//文件上传
			}else{


			    // 移动到框架应用根目录/public/uploads/ 目录下
			    
			    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'video'. DS .$videotype);

			    if($info){

			       /*$arr=array(
			       		'path'=>'public' . DS . 'uploads' . DS . 'video'. DS .$videotype. DS .$info->getSaveName(),
			       		'status'=>"1"
			       );*/
			       $arr=array(
				   		'path'=>DS. 'public' . DS . 'uploads' . DS . 'video'. DS .$videotype . DS . $info->getSaveName(),
				   		'status'=>'1',
				   		'filename'=>$info->getInfo()['name'] //文件原来的名称
				   	);
			       return json($arr);
			    }else{
			    	return json($file->getError());
			    }


			}
	    }
	
	}
	#上传视频#
	public function videoupload(){

  		
		

	
	}
	#管理员登录记录#
	protected function log($text){

		$data=array(
			'adminid'=>session('admin.id'),
			'name'=>session('admin.nickname'),
			'content'=>"管理员:".session('admin.nickname')."__".$text,
			'addtime'=>date('Y-m-d H:i:s',time())
		);
		
		Db::name('admin_log')->insert($data);

	}
	
	
	
	

	#阿里大鱼短信验证码# //传入手机号
	public function yzm($tel){
        //阿里大鱼
        //$tel=input('post.tel');
        $num=rand(10000,99999);
        session('yzm',$num);
        session('tel',$tel);
       
        Loader::import('ali.TopSdk', EXTEND_PATH);
        $c = new \TopClient;
        $c->appkey = "";
        $c->secretKey = "";
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("123456");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName("网站");
        $req->setSmsParam("{\"number\":\"".$num."\"}");
        $req->setRecNum($tel);
        $req->setSmsTemplateCode("SMS_56230225");
        $resp = $c->execute($req);
       
    }

  

    #获取所有页面模板#
    public function gettemplate(){
    	
    	//目录下文件
		$template=array("0"=>array(),"1"=>array(),"2"=>array() );

	    foreach (scandir($this->dir) as $k => $v) {
	    	//单页
	       if(preg_match("/page_(.*?).html/",$v,$r)==true){

	            array_push($template[0],str_replace(".html","",$v));
	        //列表页
	       }else if(preg_match("/list_(.*?).html/",$v,$r)==true){
	       		array_push($template[1],str_replace(".html","",$v));
	        //内容页
	       }else if(preg_match("/content_(.*?).html/",$v,$r)==true){
	       		array_push($template[2],str_replace(".html","",$v));
	       }

	    }

	    return $template;
    }
    
	
	




}
